<template>
	<!-- 设置 -->
	<view class="header">
		<!-- 头部 -->
		<hx-navbar ref="hxnb" :config="config" />
		<u-cell-group style="background-color: #fff;">
			<u-cell title="修改信息" :isLink='true' @click="personalInformation()">
				<view slot="value" class="u-slot-value">
				</view>
			</u-cell>
			<u-cell title="手机号" :isLink='true'>
				<view slot="value" class="u-slot-value">
					{{setting.phone}}
				</view>
			</u-cell>
			<u-cell title="消息通知" :isLink='true'></u-cell>
			<!-- <u-cell title="我的收货地址" :isLink='true' @click="addressManagement()"></u-cell> -->
			<u-cell title="清理缓存" :isLink='true' @click="cellClick()">
				<view slot="value" class="u-slot-value">
					{{fileSize}}
				</view>
			</u-cell>
			<u-cell title="关于" :isLink='true' url="concerning"></u-cell>
			<u-cell title="切换角色" :isLink='true' url="/pages/login/identity"></u-cell>
			<u-cell title="退出登录" :isLink='true' @click="goHome()"></u-cell>
		</u-cell-group>
		<view class="cu-modal" :class="modalName == 'hint' ?  'show' : ''">
			<view class="cu-dialog" style="border-radius: 25rpx;width: 600rpx;">
				<view class="bg-white flex flex-direction align-center pingfang-Bold"
					style="padding: 81rpx 44rpx 75rpx;color: #191919;">
					你当前缓存为{{fileSize}},清理缓存后，在平台下载的文件会被清空，请确认
				</view>
				<view class="flex" style="height: 100rpx;font-size: 32rpx;border-top: 1px solid #D8D8D8;">
					<view class="flex-sub flex justify-center" @click="modalName = ''"
						style="line-height: 100rpx;background-color: #FFFFFF;">取消</view>
					<view class="flex-sub flex justify-center"
						style="background-color: #FF7B2C;line-height: 100rpx;color: #FFFFFF;" @click="clear">确认</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				config: {
					title: '设置',
					color: '#111111',
					backgroundColor: [1, '#f3f4f6'],
					border: true,
					// #ifdef H5
					backPage: '',
					// #endif
				},
				fileSize: '',
				modalName: '',
				setting: {
					name: '',
					phone: ''
				}
			}
		},
		created() {
			// this.clearCache();
		},
		onLoad(e) {
			if (e.userName) {
				this.setting.name = e.userName
			} else if (e.shopsName) {
				this.setting.name = e.shopsName
			} else if (e.name) {
				this.setting.name = e.name
			}
			this.setting.phone = e.phone
		},
		methods: {
			// 个人信息
			personalInformation() {
				this.$tools.goPageNoAuth('../../../clientEnd/page/Information/personalInformation')
			},
			goHome() {
				// 防止toast不关闭卡住
				uni.hideToast()
				this.$tools.showToast('退出登陆成功...', 1000)
				this.$cache.clearCache()
				setTimeout(() => {
					uni.reLaunch({
						url: '/pages/login/login',
						animationType: 'zoom-fade-out',
						animationDuration: 500,
					})
				}, 1000)
			},
			cellClick() {
				this.modalName = 'hint'
			},
			clear() {
				plus.cache.clear(() => {
					this.clearCache();
					this.$forceUpdate();
				})
				let url = (plus.os.name == "Android" ? 'file://storage/emulated/0/' + this.configText.downLoadBaseUrl +
					'/' : '_downloads/' + this.configText.downLoadBaseUrl + '/')
				plus.io.resolveLocalFileSystemURL(url + "/downLoad", (entry) => {
					console.log(entry, '瓦打我')
					const directoryReader = entry.createReader();
					directoryReader.readEntries((entries) => {
						// 遍历
						entries.map(readEntry => {
							if (readEntry.isFile) {
								const fileName = readEntry.name
								const filePath = readEntry.fullPath
								plus.io.resolveLocalFileSystemURL(filePath, (file) => {
									file.remove() // 删除文件
								})
							}
						})
						console.log(JSON.stringify(e));
						uni.showToast({
							title: '清除缓存完成',
							icon: 'none',
							duration: 1000
						})
					}, (e) => {
						console.log(JSON.stringify(e));
						uni.showToast({
							title: '清除缓存完成',
							icon: 'none',
							duration: 1000
						})
					})
				}, (e) => {
					console.log(JSON.stringify(e));
					uni.showToast({
						title: '清除缓存完成',
						icon: 'none',
						duration: 1000
					})
				})
				this.modalName = ''
			},
			// 读取本地缓存大小
			clearCache() {
				let self = this;
				plus.cache.calculate(function(size) {
					if (size < 1024) {
						self.fileSize = size + 'B'
					} else if (size / 1024 >= 1 && size / 1024 / 1024 < 1) {
						self.fileSize = Math.floor(size / 1024 * 100) / 100 + 'KB'
					} else if (size / 1024 / 1024 >= 1) {
						self.fileSize = Math.floor(size / 1024 / 1024 * 100) / 100 + 'M'
					}
				})
			},
			addressManagement() {
				console.log('哈哈哈')
				this.$tools.goPageNoAuth('/clientEnd/page/plate/addressManagement')
			},
		}
	}
</script>

<style scoped>
	.header {
		background: #f3f4f6;
		height: 100vh;
	}
</style>
